package com.foreverwin.mes.meapi.service.impl;

import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.foreverwin.mes.core.base.FrontPage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.meapi.model.Item;
import com.foreverwin.mes.meapi.mapper.ItemMapper;
import com.foreverwin.mes.meapi.service.ItemService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Syngna
 * @since 2020-01-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {


    @Autowired
    private ItemMapper itemMapper;

    @Override
    public Item selectById(String handle) {
        Item item = null;
        Locale locale = LocaleContextHolder.getLocale();
        if(StringUtils.notBlank(handle) && handle.endsWith("#")) {
            List<Item> itemList = this.itemMapper.selectList(locale.getLanguage(), new EntityWrapper<Item>()
                    .eq(Item.SITE, StringUtils.trimSite(handle))
                    .eq(Item.ITEM, StringUtils.trimHandle(handle))
                    .eq(Item.CURRENT_REVISION, "true"));
            return itemList == null || itemList.size() == 0 ? null : itemList.get(0);
        }
        return this.itemMapper.selectById(locale.getLanguage(), handle);
    }

    @Override
    public Page<Item> selectPage(Page<Item> page, EntityWrapper<Item> queryWrapper) {
        Locale locale = LocaleContextHolder.getLocale();
        Wrapper wrapper = SqlHelper.fillWrapper(page, queryWrapper);
        page.setRecords(this.itemMapper.selectPage(locale.getLanguage(), page, wrapper));
        return page;
    }

    @Override
    public List<Item> selectList(Item item) {
        Locale locale = LocaleContextHolder.getLocale();
        EntityWrapper<Item> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(item);
        return this.itemMapper.selectList(locale.getLanguage(), queryWrapper);
    }

    @Override
    public List<Item> selectList(Wrapper<Item> queryWrapper) {
        Locale locale = LocaleContextHolder.getLocale();
        return this.itemMapper.selectList(locale.getLanguage(), queryWrapper);
    }

    @Override
    public Item selectItemByCode(String site, String item) {
        Item queryEntity = new Item();
        queryEntity.setSite(site);
        queryEntity.setItem(item);
        queryEntity.setCurrentRevision("true");
        List<Item> items = selectList(queryEntity);
        return items.isEmpty() ? null : items.get(0);
    }

    @Override
    public Item selectItem(String itemBo) {
        Item queryEntity = new Item();
        queryEntity.setHandle(itemBo);
        List<Item> items = selectList(queryEntity);
        return items.isEmpty() ? null : items.get(0);
    }

}